package com.zjw.forum.services.impl;

import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.dao.MessageMapper;
import com.zjw.forum.exception.ApplicationException;
import com.zjw.forum.model.Message;
import com.zjw.forum.model.User;
import com.zjw.forum.services.IMessgaeService;
import com.zjw.forum.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MessageService implements IMessgaeService {
    @Autowired
    private IUserService userService;
@Autowired
private MessageMapper messageMapper;
    @Override
    public int sendMessage(Long postUserId,Long receiveUserId, String content) {
        //校验 参数
        if(receiveUserId==null||receiveUserId<=0|| !StringUtils.hasLength(content)){
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造对象在插入
        Message message=new Message();
        message.setContent(content);
        message.setPostUserId(postUserId);
        message.setReceiveUserId(receiveUserId);
        message.setState((byte)0);
        message.setDeleteState((byte)0);
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        return messageMapper.insertSelective(message);
    }

    @Override
    public Integer notReadCount(Long receiveUserId) {
        //校验参数
        if(receiveUserId==null||receiveUserId<=0){
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询未读数
        Integer i = messageMapper.selectNotReadCount(receiveUserId);
        if(i==null){
            log.error(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return i;
    }

    @Override
    public List<Message> getAll(Long receiveUserId) {
        //校验参数
        if(receiveUserId==null||receiveUserId<=0){
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //获取站内信列表
        return messageMapper.selectByreceiveUserId(receiveUserId);
    }

    @Override
    public int updateState(Long id) {


        //更新之前创建对象
        Message message=new Message();
        message.setId(id);
        message.setState((byte)1);
        return messageMapper.updateByPrimaryKeySelective(message);
    }

    @Override
    public int replyMessage(Long loginUserId,Long repliedId, String content) {
        //参数校验
        if(repliedId==null||repliedId<=0||!StringUtils.hasLength(content)||loginUserId==null||loginUserId<=0){
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验站内信是否存在
        Message message = messageMapper.selectByPrimaryKey(repliedId);
        if(message==null||message.getDeleteState()==1){
            log.error(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //校验站内信的接收者和登录用户是否为同一个人，只能回复接收者是自己的站内信
        if(!message.getReceiveUserId().equals(loginUserId)){
            log.error(ResultCode.FAILED_MESSAGE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_BANNED));
        }
        //校验站内信的发送者用户状态是否被删除
        User userinfo = userService.getUserinfo(message.getPostUserId());
        if(userinfo.getDeleteState()==1){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //开始回复
        Message msg1=new Message();
        //发送者是登录用户
        msg1.setPostUserId(loginUserId);
        //接收者是被回复站内信的发送者
        msg1.setReceiveUserId(message.getPostUserId());
        //设置回复内容
        msg1.setContent(content);
        //初始化默认值
        msg1.setState((byte)0);
        msg1.setDeleteState((byte)0);
        Date date=new Date();
        msg1.setCreateTime(date);
        msg1.setUpdateTime(date);
        int i = messageMapper.insertSelective(msg1);
        if(i!=1){
            log.error(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //开始修改
        Message msg=new Message();
        msg.setId(repliedId);
        msg.setState((byte)2);
        return messageMapper.updateByPrimaryKeySelective(msg);
    }
}
